﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SQLite;
using FF.Method;
using FF.Entity;

namespace FF
{
    public partial class FormUserAdd : Form
    {
        public FormUserAdd()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if(textBoxUserName.Text == "" || textBoxPassword1.Text == "" || textBoxPassword2.Text == "")
            {
                MessageBox.Show("用户名或密码不能为空！", "提示");
                return;
            }
            if(textBoxPassword1.Text != textBoxPassword2.Text)
            {
                MessageBox.Show("两次输入的密码不一致！", "提示");
                return;
            }
            User user = new User();
            //user.UserId = UserMethod.generateUserID();
            user.UserName = textBoxUserName.Text.Trim();
            user.Password = textBoxPassword2.Text.Trim();
            //DataRowView selectedItem = (DataRowView)comboBox1.SelectedItem;
            //MessageBox.Show(comboBox1.SelectedValue.ToString() + "===" + selectedItem.Row["name"].ToString());
            user.Type = comboBox1.SelectedValue.ToString();
            user.Remark = richTextBoxRemark.Text.Trim();
            int addresult = UserMethod.userAdd(user);
            if(addresult == 1)
            {
                MessageBox.Show("添加用户成功！");
                this.Close();
            }else
            {
                MessageBox.Show("添加用户失败！");
            }
            //user.Type = UserMethod.getUserTypeByName(comboBox1);
        }

        private void frmAddUser_Load(object sender, EventArgs e)
        {
            //DataSet ds = SQLiteHelper.Query("select type,name from ff_user_type");
            //comboBox1.DataSource = ds.Tables[0];
            //SQLiteDataReader reader = SQLiteHelper.ExecuteReader("select name from ff_user_type");
            //if(reader.HasRows)
            //{
            //    while(reader.Read())
            //    {
            //        string typenames = reader.GetString(0);
            //        comboBox1.Items.Add(typenames);
            //        //comboBox1.Items.Add(reader.GetString(1));
            //    }
            //}
            //reader.Close();
            DataSet ds = SQLiteHelper.Query("select type,name from ff_user_type");
            comboBox1.DataSource = ds.Tables[0];
            comboBox1.DisplayMember = "name";
            comboBox1.ValueMember = "type"; 
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void groupBox1_Enter(object sender, EventArgs e)
        {

        }
    }
}
